package middleware

import (
	"leeframe/internal/pkg/trace"

	"github.com/gin-gonic/gin"
)

func TraceLog() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		requestId := ctx.GetHeader(trace.HeaderKey)
		if requestId == "" {
			requestId = trace.New("").ID()
			ctx.Request.Header.Set(trace.HeaderKey, requestId)
		}
		ctx.Set(trace.HeaderKey, requestId)
		ctx.Writer.Header().Set(trace.HeaderKey, requestId)

		ctx.Next()
	}
}
